package com.ancient.web.data1.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ancient.base.page.ResponseResult;
import com.ancient.web.data1.entity.User;
import com.ancient.web.data1.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Scope("prototype")
public class UserController {
	@Resource
	private UserService userService;
	
	
	@ApiOperation(value = "获取用户列表", httpMethod = "POST", notes = "获取用户列表", response = User.class)
	@ApiImplicitParam(name = "user", value = "请求参数", required = true, paramType = "body", dataType = "User")
	@PostMapping(value = "/getUser")
	public List<User> getUsers(@RequestBody User user){
		List<User> list = userService.getUsers(user);
		return list;
	}
	
	@ApiOperation(value = "获取用户列表(带分页)", httpMethod = "POST", notes = "获取用户列表(带分页)", response = User.class)
	@ApiImplicitParam(name = "user", value = "请求参数", required = true, paramType = "body", dataType = "User")
	@PostMapping(value = "/user_list")
	public ResponseEntity<ResponseResult<List<User>>> userList(@RequestBody User user){
		return ResponseEntity.ok(userService.userList(user));
	}
	
	@ApiOperation(value = "新增用户", httpMethod = "POST")
	@ApiImplicitParam(name = "user", value = "新增用户", required = true, paramType = "body", dataType = "User")
	@PostMapping(value = "/addUser")
	public String addUser(@RequestBody User user){
		userService.addUser(user);
		return "success";
	}
	
	@ApiOperation(value = "根据id获取用户", httpMethod = "GET", notes = "根据id获取用户", response = User.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", dataType = "String", defaultValue = "101469169305247746") })
	@RequestMapping(value="getUser/{id}", method = RequestMethod.GET)
	public User getUsersById(@PathVariable("id")String id){
		User user = new User();
		user.setId(id);
		List<User> list = userService.getUsers(user);
		return list.get(0);
	}
	
	@ApiOperation(value = "删除用户", httpMethod = "DELETE", notes = "删除用户", response = String.class)
	@RequestMapping(value="delUserById/{id}", method = RequestMethod.DELETE)
	public String delUserById(@PathVariable("id")int id){
		userService.delUserById(id);
		return "succes";
	}

}
